import sublime
import sublime_plugin
import time

##
# 功能：选中的内容逐行替换时间戳为 年月日
# 例如：
# 指令：view.run_command('example_test')
# 1710842228 => 1710842228 2024-03-19 17:57:08
# 1710842227 => 1710842227 2024-03-19 17:57:07
#
class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        selections = self.view.sel()
        for s in selections:
            if s.empty() or s.size() <= 1:
                break
            lines = self.view.lines(s)
            for line in reversed(lines):
                curstrline = self.view.substr(line)
                curstrline = curstrline.strip()
                print(curstrline)
                if not curstrline.isdigit():
                    continue
                if len(curstrline) > 10:
                    curstrline = curstrline[0:10]
                timeArray = time.localtime(int(curstrline))
                otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
                self.view.insert(edit, line.end(), " " + otherStyleTime)